package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.*;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.mapper.ApplicationRecordMapper;
import com.woniuxy.factory.mapper.EmpMapper;
import com.woniuxy.factory.mapper.PurchaseplanMapper;
import com.woniuxy.factory.service.PurchaseplanService;
import com.woniuxy.factory.util.ApplicationType;
import com.woniuxy.factory.util.AuditType;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseplanServiceImp implements PurchaseplanService {
    @Autowired
    PurchaseplanMapper purchaseplanMapper;
    @Autowired
    EmpMapper empMapper;
    @Autowired
    ApplicationRecordMapper applicationRecordMapper;
    @Override
    public ResponseResult<List<Purchaseplan>> getpurchaseplans(Purchaseplan purchaseplan, int page, int limit) {
        //查询条数
        int count= purchaseplanMapper.getCount(purchaseplan);
        int home = (page-1)*limit;
        List<Purchaseplan> purchaseplans = purchaseplanMapper.selectPurchaseplans(purchaseplan,home,limit);
        for (int i=0;i<purchaseplans.size();i++){
            if (purchaseplans.get(i).getApplicationRecord().getApplystatus()==AuditType.No_Approval){
                purchaseplans.get(i).setStatus("未审核");
            }else if (purchaseplans.get(i).getApplicationRecord().getApplystatus()==AuditType.ALREADY_PASSED){
                purchaseplans.get(i).setStatus("已通过");
            }

        }
        return new ResponseResult<>(0,"查询成功",count,purchaseplans);
    }

    @Override
    public ResponseResult<Map> getpurchaseplanById(String pupId) {
        Map<String,Object> map = new HashMap<>();
        //查询指定计划的详情
        Purchaseplan purchaseplan = new Purchaseplan();
        purchaseplan.setPupId(pupId);
        List<Purchaseplan> purchaseplans = purchaseplanMapper.selectPurchaseplans(purchaseplan, 0, 3);
        //判断当前计划状态
        try {
            getStatus(purchaseplan);
        }catch (BusinessException e){
            return new ResponseResult(0,e.getMessage(),null,null);
        }
        map.put("purchaseplan",purchaseplans.get(0));
        return new ResponseResult<>(0,"查询成功",null,map);
    }

    @Override
    public ResponseResult updatePurchaseplan(Purchaseplan purchaseplan,String empId) {
        //判断当前计划状态
        try {
            getStatus(purchaseplan);
        }catch (BusinessException e){
            return new ResponseResult(0,e.getMessage(),null,null);
        }
        //修改指定计划
        int i = purchaseplanMapper.updatePurchaseplan(purchaseplan,empId);
        if (i==0){
            throw new BusinessException("修改失败");
        }
        return new ResponseResult(0,"修改成功",null,null);
    }

    @Override
    public ResponseResult deletepurchaseplan(String pupId) {
        //查询指定计划的详情
        Purchaseplan purchaseplan = new Purchaseplan();
        purchaseplan.setPupId(pupId);
        //判断当前计划状态
        try {
            getStatus(purchaseplan);
        }catch (BusinessException e){
            return new ResponseResult(1,e.getMessage(),null,null);
        }
        int i =purchaseplanMapper.deletepurchaseplan(pupId);
        if (i==0){
            return new ResponseResult(1,"删除失败",null,null);
        }
        return new ResponseResult(0,"删除成功",null,null);
    }

    @Override
    @Transactional
    public ResponseResult addPurchasePlan(Purchaseplan purchaseplan, String empId, HttpServletRequest request) {
        //新增采购计划
        purchaseplan.setPupId(UuidUtil.get16UUID());
        Emp emp=new Emp();
        emp.setEmpId(empId);
        purchaseplan.setEmp(emp);
        int i = purchaseplanMapper.insertPurchasePlan(purchaseplan);
        if (i==0){
            throw new BusinessException("新增失败");
        }
        //新增采购计划申请
        ApplicationRecord applicationRecord = new ApplicationRecord();
        applicationRecord.setAprId(UuidUtil.get16UUID());
        applicationRecord.setApplicationtype(ApplicationType.PURCHASEPLAN);
        applicationRecord.setApplicationnum(purchaseplan.getPupId());
        int o =applicationRecordMapper.insertApplicationRecord(applicationRecord);
        if (o==0){
            throw new BusinessException("记录插入失败");
        }
        return new ResponseResult(0,"新增成功",null,null);
    }

    //判断计划状态
    public void getStatus(Purchaseplan purchaseplan){
        List<Purchaseplan> purchaseplans = purchaseplanMapper.selectPurchaseplans(purchaseplan, 0, 3);
        //判断当前计划状态
        try {
            if (purchaseplans.get(0).getApplicationRecord().getApplystatus()!= AuditType.No_Approval){
                throw new BusinessException("计划状态已改变，不能修改");
            }
        }catch (NullPointerException e){
            throw new BusinessException("计划不存在");
        }
    }

}
